/*
 * ttbqt - A teletekst browser
 * http://code.google.com/p/ttbqt
 * Copyright (C) 2011, Kevin van der Vlist
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include <QComboBox>
#include <QPixmap>
#include <QSettings>
#include <QString>
#include <QtGui>

#include <exception>
#include <iostream>
#include <map>

#include "MainWindow.h"

/**
 * \class MainWindow
 *
 * \short Class with the main window
 *
 * Creates and holds all classes needed by ttbqt
 */

/**
 * \short The constructor of the main window
 * \param a A pointer to the QApplication that created the main window.
 * 
 * Useful when slotting close events
 */


MainWindow::MainWindow(QApplication *a) {
  app = a;
  display = new PageDisplay();
  pageinfo = new PageInfo(display);
  buttonbar = new ButtonBar();
  std::map<QString, Teletext *> *cm = pageinfo->getChannelMap();
  std::map<QString, Teletext *>::iterator it;
  for(it = cm->begin(); it != cm->end(); it++) {
    it->first;
    buttonbar->addChannel(it->first);
  }
  // Check if there are any settings from a previous run
  QSettings settings(QString(AUTHOR), QString(APPNAME));
  if(settings.value("CHANNEL").toString() != NULL) {
    pageinfo->setTeletextChannel(settings.value("CHANNEL").toString());
    buttonbar->setChannel(settings.value("CHANNEL").toString());
  }

  if(settings.value("PAGE").toInt() != pageinfo->getPage()) {
    pageinfo->setPage(settings.value("PAGE").toInt());
  }

  if(settings.value("SUBPAGE").toInt() != pageinfo->getSubPage()) {
    pageinfo->setSubPage(settings.value("SUBPAGE").toInt());
  }

  pageinfo->init();

  controller = new Controller(pageinfo, buttonbar);

  dockbuttonbar = new QDockWidget();
  // Remove stupid features:
  dockbuttonbar->setFeatures(QDockWidget::DockWidgetClosable);
  dockbuttonbar->setFeatures(QDockWidget::DockWidgetMovable);
  dockbuttonbar->setAllowedAreas(Qt::BottomDockWidgetArea);
  dockbuttonbar->setWidget(buttonbar);

  setCentralWidget(display);

  addDockWidget(Qt::BottomDockWidgetArea, dockbuttonbar);

  // Connect all the slots in the buttonbar to the controller
  buttonbar->connectSlots(controller);
  buttonbar->connectQuit(this);
}

MainWindow::~MainWindow() {
  saveQuit();
  delete display;
  delete pageinfo;
  delete buttonbar;
  delete controller;
  delete dockbuttonbar;
}

void MainWindow::closeEvent(QCloseEvent *event) {
  saveQuit();
  event->accept();
}

void MainWindow::saveQuit() {
  QSettings settings(QString(AUTHOR), QString(APPNAME));
  settings.setValue("CHANNEL", pageinfo->getTeletextChannel());
  settings.setValue("PAGE", pageinfo->getPage());
  settings.setValue("SUBPAGE", pageinfo->getSubPage());
  settings.setValue("GEOMETRY", saveGeometry());

  app->quit();
}
